package com.xilihui.xlh.business.activitys.store;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.CommentEntity;
import com.xilihui.xlh.business.requests.StoreRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.component.recyclerview.LinearLayoutColorDivider;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/9/4.
 */

public class EvaluationActivity extends BaseCompatActivity implements OnRefreshListener, OnLoadMoreListener {
    
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    
    BaseAdapter<CommentEntity.CommentsBean> baseAdapter;
    ArrayList<CommentEntity.CommentsBean> data = new ArrayList<>();
    private String goods_id = "";
    private int page = 1;

    @Override
    public int getLayout() {
        return R.layout.activity_evaluation;
    }

    @Override
    public String initTitle() {
        return "全部评价";
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        
        goods_id = getIntent().getStringExtra("goods_id");
        
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadMoreListener(this);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new LinearLayoutColorDivider(getResources(),R.color.view_line,
                R.dimen.divider_line, LinearLayout.VERTICAL));
        
        
        baseAdapter = new BaseAdapter<CommentEntity.CommentsBean>(this,data) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_store_evaluate;
            }

            @Override
            protected void getView(BaseViewHolder holder, CommentEntity.CommentsBean entity, int position) {
                SimpleDraweeView simpleDraweeView = (SimpleDraweeView)holder.getView(R.id.sdv);
                simpleDraweeView.setImageURI(UrlConst.baseUrl() + entity.getUser().getHead_pic());
                
                TextView tv_username = (TextView)holder.getView(R.id.tv_username);
                tv_username.setText(entity.getUser().getNickname());
                String zan_type = entity.getZan_num();
                Drawable drawable = null;
                if(zan_type.equals("0")){
                    drawable = getResources().getDrawable(R.mipmap.ic_eva_bad);
                }else if(zan_type.equals("1")){
                    drawable = getResources().getDrawable(R.mipmap.ic_eva_normal);
                }else if(zan_type.equals("2")){
                    drawable = getResources().getDrawable(R.mipmap.ic_eva_good);
                }
                drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
                tv_username.setCompoundDrawables(null,null,drawable,null);
                
                holder.setText(R.id.tv_time,entity.getAdd_time());
                holder.setText(R.id.tv_pingjia,entity.getContent());
            }
        };
        
        recyclerView.setAdapter(baseAdapter);
        baseAdapter.setAnimationsLocked(true);
        getData(true);
    }

    @Override
    public void initData() {

    }
    
    public void getData(boolean isLoading){
        StoreRequest.comment(goods_id,page)
                .compose(DoTransform.<CommentEntity>applyScheduler(this,isLoading))
                .compose(this.<CommentEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<CommentEntity>(this) {
                    @Override
                    protected void onSuccess(CommentEntity entity) {
                        
                        if(page==1){
                            data.clear();
                        }

                        data.addAll(entity.getComments());
                        baseAdapter.setList(data);
                        if(page>=entity.getPageCount()){
                            smartRefreshLayout.finishLoadMoreWithNoMoreData();
                        }else {
                            smartRefreshLayout.setNoMoreData(false);
                        }
                        
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                        
                    }

                    @Override
                    protected void onFail(String msg) {
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                    }
                });
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 1;
        getData(false);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page = page + 1;
        getData(false);
    }
    
        
}
